#import "FileCell.h"
#import <appkit/NXImage.h>
#import <appkit/Application.h>
#import <appkit/View.h>
#import <dpsclient/dpsNeXT.h>
#import <appkit/NXHelpPanel.h>
#import "MenuManager.h"

@implementation FileCell: Cell
{ id controlView, image ;
  char fileName[128] ;
}

- calcCellSize:(NXSize *)aSize ;
{ aSize->height = aSize->width = 48.0;
  return self;
}

- drawSelf: (const NXRect *)cellFrame inView:controlView ;
{ char *directory,*file,*fullPath ;
  int pathLength ;
  id controlWindow ;
  NXPoint origin = cellFrame->origin ;
  origin.y += cellFrame->size.height ;
  controlWindow = [controlView window] ;
  if([controlWindow isKindOf: [NXHelpPanel class]])
  { // pain in the butt to get filename from a help panel
    directory = [controlWindow helpDirectory] ;
    file = [controlWindow helpFile] ;
    pathLength = strlen(directory) + strlen(file) + strlen(fileName) ;
    fullPath = (char *) alloca(pathLength + 3) ;
    sprintf(fullPath,"%s/%s/%s",directory,file,fileName) ;
  }
  else
  { // much easier to get it from a workspace
    directory = [controlWindow fileName] ;
    pathLength = strlen(directory) + strlen(fileName) ;
    fullPath = (char *) alloca(pathLength + 2) ;
    sprintf(fullPath,"%s/%s",directory,fileName) ;
  }
  support = [[Application workspace] getIconForFile: fullPath] ;
  [support composite:NX_COPY toPoint:&origin] ;
  return self ;
}

- highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag;
{
#ifdef DEBUG
  fprintf(stderr,"FileCell.m  highlight inView lit\n");
#endif DEBUG
  return self;
}

- initFromFile: (char *) filename controlView: aView image: anImage ;
{ char *index ;
  [super init] ;
  index = rindex(filename,"/") ; // point index at filename minus
  index++ ; // the path
  strcpy(fileName, index) ; // copy into the ivar
  controlView = aView ;
  image = anImage ;
  return self ;
}

- (BOOL)trackMouse:(NXEvent *)theEvent inRect:(const NXRect *) cellFrame ofView:aView;
{ static	long lastDown = 0 ; 
  // if the user clicks down in less than .33 secs (20 intervals)
  // then lets treat it as a double click
  if (theEvent->time-lastDown < 20)
  { id helpPanel ;
    char *helpPath, *helpFile, *fullPath ;
    int pathLength ;
    helpPanel = [controlView window] ;
    helpPath = [helpPanel helpDirectory] ;
    helpFile = [helpPanel helpFile] ;
    pathLength = strlen(helpPath) + strlen(helpFile) + strlen(fileName) ;
    fullPath = (char *) alloca(pathLength + 1) ;
    sprintf(fullPath,"%s/%s/%s",helpPath,helpFile,fileName) ;
    [[Application workspace] openFile:fullPath] ;
  }
  lastDown = theEvent->time;
  // should allow drag out here 
  return YES;
}

// read attached filename and fetch its Image
// should test for errors 
- readRichText:(NXStream *)stream forView:view ;
{ NXScanf(stream, "%s\n", fileName) ; 
  controlView = view ;
  return self ;
}

// just write out the name of the attached file
- writeRichText:(NXStream *)stream forView:view ;
{ NXPrintf(stream, "%s\n",fileName);
  return self;
}


